package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.common.util.Consts;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    public static void addCookie(HttpServletResponse response, String tokenCookieName, String token) {
        Cookie cookie = new Cookie(tokenCookieName, token);
        cookie.setPath("/");
        cookie.setMaxAge((int)(Consts.USER_INFO_TOKEN_TIME*60));
        response.addCookie(cookie);
    }

    public static Cookie getCookie(HttpServletRequest request, String tokenCookieName) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals(tokenCookieName)){
                    return cookie;
                }
            }
       }
        return null;
    }
}
